<template>
  <div class="register-page">
    <!-- 背景容器 -->
    <div class="bg-container">
      <img src="@/assets/bg.png" alt="" class="bg-img" />
      <!-- 注册标题 -->
      <div class="register-title">
        <h3>加入钞能力搬运工人端</h3>
      </div>

      <!-- 注册表单 -->
      <div class="form-container">
        <van-form @submit="onSubmit">
          <!-- 手机号 -->
          <div class="form-item">
            <div class="form-label">手机号</div>
            <van-field v-model="formData.phone" placeholder="请输入手机号" type="tel" maxlength="11" :rules="[{ required: true, message: '请输入手机号' }]" />
          </div>          
          <!-- 验证码 -->
          <div class="form-item">
            <div class="form-label">验证码</div>            
              <van-field v-model="formData.verifyCode" placeholder="请输入验证码" :rules="[{ required: true, message: '请输入验证码' }]">
                <template #button>
                  <van-button class="send-code-btn" :disabled="codeCountdown > 0" size="mini" @click="sendVerifyCode">
                    {{ codeCountdown > 0 ? `${codeCountdown}s` : "发送验证码" }}
                  </van-button>
                </template>
              </van-field>          
          </div>

          <!-- 邀请码 -->
          <div class="form-item">
            <div class="form-label">邀请码</div>
            <van-field v-model="formData.inviteCode" placeholder="8888" readonly disabled />
          </div>

          <!-- 提交按钮 -->
          <div class="submit-container">
            <van-button class="submit-btn" type="primary" size="large" :loading="submitting" native-type="submit"> 完成注册 </van-button>
            <div class="super-small-text-container description">完成注册后会自动跳转【应用宝】下载页面，您也可以通过手机应用市场搜素“钞能力搬运工人端”下载安装</div>
          </div>
        </van-form>
      </div>
      <!-- 底部说明文字 -->
      <div class="bottom-text">
        <p class="disclaimer-text super-small-text-container">
          本宣传物料所载信息仅供参考，不构成任何形式的承诺或保证。我们已尽力确保信息的准确性和完整性，但不对因使用本宣传物料所引起的任何直接或间接损失承担责任。最终解释权归钞能力搬运平台所有。
        </p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { showToast, showSuccessToast, Form,showFailToast } from "vant";
import request from "../../api/request";
import requestCenter from "../../api/requestCenter";

const router = useRouter();
const route = useRoute();

// 表单数据
const formData = ref({
  phone: "",
  password: "",
  confirmPassword: "",
  verifyCode: "",
  inviteCode: "",
});

// 验证码倒计时
const codeCountdown = ref(0);
const submitting = ref(false);

onMounted(() => {
  // 从路由参数中获取邀请码
  formData.value.inviteCode = route.query.code || "8888";
});

// 发送验证码
const sendVerifyCode = () => {
  if (!formData.value.phone) {
    showToast("请先输入手机号");
    return;
  }

  if (!/^1[3-9]\d{9}$/.test(formData.value.phone)) {
    showToast("请输入正确的手机号");
    return;
  }

  // 开始倒计时
  codeCountdown.value = 60;
  const timer = setInterval(() => {
    codeCountdown.value--;
    if (codeCountdown.value <= 0) {
      clearInterval(timer);
    }
  }, 1000);
  request.post('api/sms/send',{'mobile': formData.value.phone,'event':'login'})
  // 这里可以调用发送验证码的API
  showSuccessToast("验证码已发送");
};

// 表单提交
const onSubmit = async (values) => {
  
  if (!formData.value.verifyCode) {
    showToast("请输入验证码");
    return;
  }

  // 开始提交
  submitting.value = true;  
  let from = new FormData()
  from.append('mobile',formData.value.phone)
  from.append('captcha',formData.value.verifyCode)  
  // let rest = await request.post('api/user/mobilelogin',from)
  // console.log(rest)
  // if(rest.code != 1){
  //   showToast(rest.msg);
  //   submitting.value = false;  
  //   return 
  // }
  inviteConpon()
  // 模拟API调用
  setTimeout(() => {
    submitting.value = false;
    showSuccessToast("注册成功");
    // 跳转到成功页面
    router.push("/register-success");
  }, 2000);
};
const inviteConpon = async () => {
  let smsLogin ={   
      mobile:formData.value.phone,    
  }
  let inviteUser = await requestCenter.post("app-api/member/auth/sms-login",smsLogin)
  localStorage.setItem("accessToken",inviteUser.data.accessToken)
  console.log("inviteUser",inviteUser)
  //代金券
  let receiveCouponParam = {
    "code":"111",
    "mobile":"15136175246",
    "shareCouponCode":"111",
  }
  //被分享人领取优惠券
  requestCenter.post("app-api/coupon/shareReceive",receiveCouponParam)  
}
const inviteUser = async () => {
  let userInfo = request.post('api/user/getUserByInviteCode',{'inviteCode': formData.value.inviteCode})
  let smsLogin ={   
      mobile: userInfo.phone,    
    }
  await requestCenter.post("app-api/member/auth/sms-login",smsLogin)
}
</script>

<style lang="less" scoped>
.register-page {
  min-height: 100vh;
  background: linear-gradient(#d83649, #87302c);
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

.bg-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 40px 40px 40px;
}

.bg-img {
  margin-top: 100px;
  width: 90%;
  height: 100%;
  object-fit: cover;
}

.register-title {
  margin: 40px 0;
  color: white;
  text-align: center;

  h3 {
    font-size: 56px;
    font-weight: bold;
    margin: 0;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
  }
}

.form-container {
  flex: 1;

  .form-item {
    display: flex;
    align-items: center;
    margin-bottom: 60px;

    .form-label {
      width: 200px;
      color: white;
      font-size: 36px;
      font-weight: 500;
    }

    :deep(.van-field) {
      flex: 1;
      background: white;
      border-radius: 8px;
      padding: 10px 12px;
      font-size: 26px;
      border: none;

      .van-field__control {
        font-size: 26px;

        &::placeholder {
          color: #c1c1c1;
          font-size: 26px;
        }
      }
    }
  }
.send-code-btn {
      background: #4299e1;
      color: white;
      border: none;
      border-radius: 8px;
      padding: 20px 24px;
      font-size: 28px;
      font-weight: 500;
      white-space: nowrap;
      min-width: 180px;

      &:active {
        background: #3182ce;
      }

      &:disabled {
        background: #a0aec0;
        color: white;
      }
    }  
}

.submit-container {
  margin-top: 40px;
  text-align: center;

  .submit-btn {
    width: 50%;
    height: 60px;
    border-radius: 40px;
    background: #40b34f;
    border: none;
    font-size: 40px;
    font-weight: bold;
    color: white;

    &:active {
      background: #2f855a;
    }
  }

  .description {
    width: 125%;
    margin-top: 10px;
    color: #fff;
    font-weight: bold;
  }
}

.bottom-text {
  .disclaimer-text {
    width: 125%;
    color: #cfbdbc;
    font-size: 12px;
    line-height: 1.4;
    margin: 0;
  }
}
</style>
